package com.thrsdk.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class GameCofig {

    private static final String configName = "th_config.properties";

    public static String getAppid(Context context) {
        String appid = "";
        Properties properties = new Properties();
        try {
            properties.load(context.getAssets().open(configName));
            appid = properties.getProperty("third_id");
            if ("".equals(appid)) {
                Log.e("参数配置错误", "third_id获取失败");
                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return appid;
    }

    public static String getAppkey(Context context) {
        String appkey = "";
        Properties properties = new Properties();
        try {
            properties.load(context.getAssets().open(configName));
            appkey = properties.getProperty("third_key");
            if ("".equals(appkey)) {
                Log.e("参数配置错误", "third_key获取失败");
                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return appkey;
    }

    public static String getGamepay_back(Context context) {
        String payback = "";
        Properties properties = new Properties();
        try {
            properties.load(context.getAssets().open(configName));
            payback = properties.getProperty("gamepay_back");
            if ("".equals(payback)) {
                Log.e("参数配置错误", "payCallback地址获取失败");
                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return payback;
    }

    public static String getThirdSecret(Context context) {
        String secret = "";
        Properties properties = new Properties();
        try {
            properties.load(context.getAssets().open(configName));
            secret = properties.getProperty("third_Sec");
            if ("".equals(secret)) {
                Log.e("参数配置错误", "appsecret获取失败");
                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return secret;
    }


    public static String getUrl(Context context) {
        String secret = "";
        Properties properties = new Properties();
        try {
            properties.load(context.getAssets().open(configName));
            secret = properties.getProperty("dyurl");
            if ("".equals(secret)) {
                Log.e("参数配置错误", "dy3367url获取失败");
                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return secret;
    }

    public static String getChannelNumber(Activity context) {
        String secret = "";
        Properties properties = new Properties();
        try {
            InputStream inputStream = context.getAssets().open(configName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            properties.load(reader);
            secret = properties.getProperty("ChannelNumber");
            if ("".equals(secret)) {
                Log.e("参数配置错误", "dy渠道参数获取失败");
                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return secret;
    }

    //todo 2022 0704增加平台标识
    public static String getPlatformNumber(Activity context) {
        String secret = "";
        Properties properties = new Properties();
        try {
            InputStream inputStream = context.getAssets().open(configName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            properties.load(reader);
            secret = properties.getProperty("PlatformNumber");
            if ("".equals(secret)) {
                Log.e("参数配置错误", "dy平台参数获取失败");
//                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return secret;
    }
}
